package com.sky.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@Slf4j
public class RedisConfiguration {

    //配置redis模版工具类对象的bean,redisTemplate
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        //记录日志
        log.info(">>>>>>>>>>开始创建Redis模版工具类对象>>>>>>>>>");
        //1.创建RedisTemplate对象
        RedisTemplate redisTemplate = new RedisTemplate();
        //2.设置连接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //3.设置Redis的key的序列化方式
        redisTemplate.setKeySerializer(new StringRedisSerializer());//方便图像化展示
        /*3.设置Redis的value的序列化方式,不要设置value的序列化方式,否则复杂类型的序列化失败,比如List<Object>*/
        //redisTemplate.setValueSerializer(new StringRedisSerializer());//方便图像化展示,可能会导致类型转换异常
        //4.返回RedisTemplate对象
        return redisTemplate;

    }
}
